﻿using glb;
using PIMS.XmlModel;
using PIMS_Interface.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace PIMS
{
    /// <summary>
    /// 增加分组LOG功能，按PLC分组，未启用的PLC则隐藏对应的分组
    /// </summary>
    public partial class FormLogView : DockContent, IDebugMsg
    {
        public FormLogView()
        {
            InitializeComponent();
        }

        public void ClearLog()
        {
            try
            {
                if (listView1.Items.Count>10)
                {
                    for (int i = 0; i < 10;i++ )
                    {
                        listView1.Items.RemoveAt(i);
                    }                    
                }               
            }
            catch
            { }
        }

        public void DebugMsg(string Msg,InfoLevel level)
        {
            try
            {
                this.BeginInvoke(new Action(() =>
                {
                    listView1.Items.Add(new ListViewItem(DateTime.Now + "  " + Msg, (int)level));
                    listView1.Items[listView1.Items.Count - 1].EnsureVisible();
                    if (listView1.Items.Count > 1000)
                    {
                        listView1.Items.RemoveAt(0);
                    }
                    AutoUpdateMaxWidth(listView1, DateTime.Now + "  " + Msg);
                }));
            }
            catch 
            { }
        }

        public void DebugMsg(string Msg, Enum logGroup,InfoLevel level)
        {
            this.DebugMsg(Msg, level);
            switch (logGroup)
            {
                case LogGroup.weight:
                    this.DebugMsg(this.listViewWeight,Msg, level);
                    break;
                case LogGroup.packer:
                    this.DebugMsg(this.listViewPacker, Msg, level);
                    break;
                case LogGroup.pal:
                    this.DebugMsg(this.listViewPal, Msg, level);
                    break;
                case LogGroup.label:
                    this.DebugMsg(this.listViewLabel, Msg, level);
                    break;
                case LogGroup.other:
                    this.DebugMsg(this.listViewOther, Msg, level);
                    break;
            }
        }

        private void DebugMsg(ListView lv,string Msg, InfoLevel level)
        {
            try
            {
                this.BeginInvoke(new Action(() =>
                {
                    lv.Items.Add(new ListViewItem(DateTime.Now + "  " + Msg, (int)level));
                    lv.Items[lv.Items.Count - 1].EnsureVisible();
                    if (lv.Items.Count > 1000)
                    {
                        lv.Items.RemoveAt(0);
                    }
                    AutoUpdateMaxWidth(lv, DateTime.Now + "  " + Msg);
                }));
            }
            catch
            { }
        }

        /// <summary>
        /// 自动调整ListView列宽
        /// </summary>
        /// <param name="lv1"></param>
        /// <param name="msg"></param>
        public void AutoUpdateMaxWidth(ListView lv1, string msg)
        {
            int multi = 5;
            if (msg.Length * multi > lv1.Columns[0].Width)
            {
                lv1.Columns[0].Width = (msg.Length + 1) * multi;
            }
        }

        private void FormLogView_Load(object sender, EventArgs e)
        {
            //使用PIMSConfig.xml初始化分组LOG
            string[] plcGroup = new string[] { "weight", "packer", "pal", "label", "other" };
            for (int i = 0; i < plcGroup.Length; i++)
            {
                XPLC x = Common.pimsConfig.GetXPLCByKey(plcGroup[i]);
                switch (plcGroup[i])
                {
                    case "weight":
                        if(!String.IsNullOrEmpty(x.methodName)) this.Pageweight.Text = x.methodName;
                        if(x.count==0) this.TabControlShowHide(this.Pageweight, false);
                        break;
                    case "packer":
                        if (!String.IsNullOrEmpty(x.methodName)) this.Pagepacker.Text = x.methodName;
                        if (x.count == 0) this.TabControlShowHide(this.Pagepacker, false);
                        break;
                    case "pal":
                        if (!String.IsNullOrEmpty(x.methodName)) this.Pagepal.Text = x.methodName;
                        if (x.count == 0) this.TabControlShowHide(this.Pagepal, false);
                        break;
                    case "label":
                        if (!String.IsNullOrEmpty(x.methodName)) this.Pagelabel.Text = x.methodName;
                        if (x.count == 0) this.TabControlShowHide(this.Pagelabel, false);
                        break;
                    case "other":
                        if (!String.IsNullOrEmpty(x.methodName)) this.Pageother.Text = x.methodName;
                        if (x.count == 0) this.TabControlShowHide(this.Pageother, false);
                        break;
                }
            }

            SQLiteDBHelper.DBClear();
        }

        /// <summary>
        /// 隐藏TagPage
        /// </summary>
        /// <param name="tp"></param>
        /// <param name="OnOff"></param>
        private void TabControlShowHide(TabPage tp,bool OnOff)
        {
            if (OnOff == true)
            {
                tp.Parent = this.tabControl1;
            }
            else
            {
                tp.Parent = null;
            }

        }
    }
}
